<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'商品分类'),
);
//商品分类
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['id'])){
		$item=pdo_get('jy_wxappjiaoyib_group',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		$params['f_cat_id']=intval($item['cat_id']);
		$return['params']['id']=intval($_RQ['id']);
	}else{
		$params['cat_level']='1';
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['cat_level'])){
		$params['cat_level']=intval($_RQ['cat_level']);
		$return['params']['title']=intval($_RQ['cat_level']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_wxappjiaoyib_group',$params,array($pindex,$psize),$total,$fields,'','star desc, id desc');
	if($list){
		foreach($list as &$item){

		}
	}	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>true,'list'=>array(
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
			array('name'=>'ccate','title'=>'绑定商城分类','type'=>'select','options'=>'ccates'),
			array('name'=>'f_cat_id','title'=>'上级分类ID','type'=>'string'),
			array('name'=>'f_cat_id','title'=>'上级名称','type'=>'select','options'=>'groups','option_value'=>'cat_id'),
			array('name'=>'cat_level','title'=>'层级','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/group/updata/','title'=>'同步分类'),
		),'action'=>array(
			array('type'=>'href','op'=>'list','title'=>'下级分类'),
			array('type'=>'modal','op'=>'selectgroups','title'=>'绑定商城分类'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'分类名称'),
			array('name'=>'cat_level','width'=>15,'type'=>'radio','title'=>'层级','options'=>'cat_levels'),
		)
	);
	$return['htmls']=$htmls;
	$return['cat_levels']=array('1'=>'1级分类','2'=>'2级分类','3'=>'3级分类');
	$return['groups']=pdo_getall('jy_wxappjiaoyib_group',array('uniacid'=>$_SESSION['uniacid']),array('cat_id','title'),'cat_id');
	$return['ccates']=pdo_getall('jy_weishop_goods_group',array('mid'=>0,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}
if($_RQ['op']=='selectgroups'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_wxappjiaoyib_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$fitem=pdo_get('jy_wxappjiaoyib_group',array('cat_id'=>$item['f_cat_id'],'uniacid'=>$_SESSION['uniacid']));
		if($fitem['id'] && !$fitem['ccate']){
			exi('上级分类还未绑定商城分类！','error');
		}
		$item['pcate']=$fitem['ccate'];
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'title'=>'绑定商城分类',
		'edit'=>array(
					array('name'=>'title','title'=>'分类名称','type'=>'copy'),
					array('name'=>'ccate','title'=>'绑定商城分类','type'=>'select','options'=>'pcates','show'=>array('cat_level'=>1)),
					array('name'=>'ccate','title'=>'绑定商城分类','type'=>'select','options'=>'ccates','show'=>array('cat_level'=>2)),
					//array('type'=>'select2_line','name'=>'pcate','options'=>'pcates','title'=>'绑定商城分类','help'=>'',
					//	'name2'=>'ccate','options2'=>'cid','title2'=>'请选择二级分类','show'=>array('cat_level'=>2)
					//),
		)
	);
	$status=array('0'=>'显示','1'=>'隐藏');	
	$return['groups']=pdo_getall('jy_wxappjiaoyib_group',array('uniacid'=>$_SESSION['uniacid']),array('cat_id','title'),'cat_id');
	$return['status']=$status;
	$return['pcates']=pdo_getall('jy_weishop_goods_group',array('mid'=>0,'uniacid'=>$_SESSION['uniacid'],'parentid'=>0),array('title','id'),'id');
	foreach($return['pcates'] as $k=>&$gv){
		$gv['cid']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$gv['id']),array('title','id'),'id');
	}
	$return['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$fitem['ccate']),array('title','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'pcate'=>trim($postdata['pcate'])?:$postdata['ccate'],
		'ccate'=>trim($postdata['ccate']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_wxappjiaoyib_group',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_wxappjiaoyib_group',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_wxappjiaoyib_group',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_wxappjiaoyib_group',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='updata'){
	$this->model('goods')->upgroup();
	exi('同步类目成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择商品','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_wxappjiaoyib_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}